import Vue from 'vue'
import VueRouter from 'vue-router'
// import Index from '@/views/main/index'
import store from '../store/index'
import { getToken} from '@/utils/auth'


Vue.use(VueRouter)

const routes = [
    {
        path: '/login',
        meta: { title: '登录', noCache: true },
        component: resolve => require(['@/views/login/login.vue'], resolve),
        hidden: true
    },
    {
        path: '/404',
        component: resolve => require(['@/views/features/404'], resolve),
        hidden: true
    },
    {
        path: '/',
        redirect: { path: '/index' }
    },
    {
        path: '/index',
        component: () => import('@/views/index'),
        meta: {
            title: '首页'
        }
    },
    {
        path: '/main',
        // component: Index,
        component: () => import('@/views/main/index'),
        children: [
            {
                path: '/IndexMain',
                name: 'IndexMain',
                meta: {
                    title: '首页',
                    footer: 1
                },
                component: resolve => require(['@/views/main/IndexMain/index'], resolve)
            },
            {
                path: '/prediction/Measured',
                name: 'predictionMeasured',
                component: () => import('@/views/main/prediction/Measured'),
                meta: {
                    title: '预报-降雨信息'
                }
            },
            {
                path: '/prediction/ShortPro/index',
                component: () => import('@/views/main/prediction/ShortPro/index'),
                meta: {
                    title: '预报-预报分析'
                }
            },
            {
                path: '/earlyWarning/EarlyWarningInfo/index',
                component: () => import('@/views/main/earlyWarning/EarlyWarningInfo/index'),
                meta: { title: '预警-预警信息' }
            },
            {
                path: '/earlyWarning/EarlyWarningStatistics',
                component: () => import('@/views/main/earlyWarning/EarlyWarningStatistics/index'),
                meta: { title: '预警-预警统计', map: '2' }
            },
            {
                path: '/Rehearsal',
                name: 'Rehearsal',
                meta: {
                    title: '预演',
                    footer: 1
                },
                component: resolve => require(['@/views/main/Rehearsal/index'], resolve)
            },
            // {
            //     path: '/reserveplan',
            //     name: 'reserveplan',
            //     meta: {
            //         title: '预案',
            //         footer: 1 // 没有底部
            //     },
            //     component: resolve => require(['@/views/main/reserveplan/index'], resolve)
            // },
            {
                path: '/reserveplan/index',
                component: () => import('@/views/main/reserveplan/index'),
                meta: { title: '预案-可视化预案' }
            },
            {
                path: '/reserveplan/visualization',
                name: 'ReserveplanVisualization',
                component: () => import('@/views/main/reserveplan/visualization'),
                meta: { title: '预案-可视化预案' }
            },
            {
                path: '/reserveplan/previewPlan',
                name: 'ReserveplanPreviewPlan',
                component: () => import('@/views/main/reserveplan/previewPlan'),
                meta: { title: '预案-预演预案管理' }
            },
        ]
    },
    // {
    //     path: '/uemain',
    //     component: () => import('@/views/uemain/index'),
    //     children: [
    //         {
    //             path: '/home',
    //             name: 'home',
    //             meta: {
    //                 title: '首页',
    //                 footer: 1
    //             },
    //             component: resolve => require(['@/views/uemain/home/index'], resolve)
    //         },
    //     ]
    // },
    {
        path: '*',
        redirect: '/404',
        hidden: true
    },
    {
        path: '/reserveplan/reserveplanReport',
        component: () => import('@/views/main/reserveplan/reserveplanReport'),
        meta: { title: '预案报告' }
    },
    {
        path: '/reserveplan/reportReservePlan',
        component: () => import('@/views/main/reserveplan/previewPlanReport'),
        meta: { title: '预案报告' }
    }
]

const router = new VueRouter({
    mode: 'history',
    base: '/',
    routes
})
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = to.meta.title
    }
    if (to.path == '/index' || to.path == '/earlyWarning/EarlyWarningInfo/index' || to.path == '/Rehearsal') { // 判断是否是陕西省平台过的 省级平台内嵌四预系统 /Rehearsal
        const pageType = to.query.pageType ? to.query.pageType : ''
        if (pageType == 'provincialLevel') { // 是省级平台 跳转过来，记录标识
            store.dispatch('setPageType', pageType);
            store.dispatch('setThirdPageUser', to.query);
            const basincode = to.query.basin ? to.query.basin : '';
            if (basincode) {
                const basinName = to.query.basinName ? to.query.basinName : '';
                store.dispatch('basin', basincode);
                store.dispatch('basinName', basinName);
            }


        }
    }
    if (getToken()) {
        if (to.path === '/login') {
            next({ path: '/' })
        } else {
            next()
        }
    } else {
        if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单，直接进入
            next()
        } else {
            next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
        }
    }

    next()
})

// 解决编程式路由往同一地址跳转时报错  vue-router@3.0版本
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
export default router

